package org.springsource.examples.crm.services.jdbc;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import org.springsource.examples.crm.model.Customer;
import org.springsource.examples.crm.services.config.CrmConfiguration;

@Configuration
public class JdbcConfiguration extends CrmConfiguration {
	@Bean
	public JdbcTemplate jdbcTemplate() {
		DataSource ds = dataSource(); // this comes from the parent class
		return new JdbcTemplate(ds);
	}

	@Bean
	public PlatformTransactionManager transactionManager() {
		DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
		dataSourceTransactionManager.setDataSource(this.dataSource());
		return dataSourceTransactionManager;
	}

	@Bean
	public TransactionTemplate transactionTemplate() {
		TransactionTemplate tt = new TransactionTemplate();
		tt.setTransactionManager(this.transactionManager());
		return tt;
	}

	@Autowired
	private TransactionTemplate transactionTemplate;

	public Customer getCustomerById(final long id) {
		return this.transactionTemplate.execute(new TransactionCallback() {
			public Customer doInTransaction(TransactionStatus status) {
				return null;
			}
		});
	}

	public Customer createCustomer(final String firstName, final String lastName) {
		return this.transactionTemplate.execute(new TransactionCallback() {
			public Customer doInTransaction(TransactionStatus status) {
				return null;
			}
		});
	}

}
